<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>CREATE OPERATOR CLASS</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="CREATE OPERATOR"
HREF="sql-createoperator.html"><LINK
REL="NEXT"
TITLE="CREATE OPERATOR FAMILY"
HREF="sql-createopfamily.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="CREATE OPERATOR"
HREF="sql-createoperator.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="CREATE OPERATOR FAMILY"
HREF="sql-createopfamily.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-CREATEOPCLASS"
></A
>CREATE OPERATOR CLASS</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN66314"
></A
><H2
>Name</H2
>CREATE OPERATOR CLASS&nbsp;--&nbsp;define a new operator class</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN66319"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>CREATE OPERATOR CLASS <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> [ DEFAULT ] FOR TYPE <TT
CLASS="REPLACEABLE"
><I
>data_type</I
></TT
>
  USING <TT
CLASS="REPLACEABLE"
><I
>index_method</I
></TT
> [ FAMILY <TT
CLASS="REPLACEABLE"
><I
>family_name</I
></TT
> ] AS
  {  OPERATOR <TT
CLASS="REPLACEABLE"
><I
>strategy_number</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>operator_name</I
></TT
> [ ( <TT
CLASS="REPLACEABLE"
><I
>op_type</I
></TT
>, <TT
CLASS="REPLACEABLE"
><I
>op_type</I
></TT
> ) ] [ FOR SEARCH | FOR ORDER BY <TT
CLASS="REPLACEABLE"
><I
>sort_family_name</I
></TT
> ]
   | FUNCTION <TT
CLASS="REPLACEABLE"
><I
>support_number</I
></TT
> [ ( <TT
CLASS="REPLACEABLE"
><I
>op_type</I
></TT
> [ , <TT
CLASS="REPLACEABLE"
><I
>op_type</I
></TT
> ] ) ] <TT
CLASS="REPLACEABLE"
><I
>function_name</I
></TT
> ( <TT
CLASS="REPLACEABLE"
><I
>argument_type</I
></TT
> [, ...] )
   | STORAGE <TT
CLASS="REPLACEABLE"
><I
>storage_type</I
></TT
>
  } [, ... ]</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66336"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>CREATE OPERATOR CLASS</TT
> creates a new operator class.
   An operator class defines how a particular data type can be used with
   an index.  The operator class specifies that certain operators will fill
   particular roles or <SPAN
CLASS="QUOTE"
>"strategies"</SPAN
> for this data type and this
   index method.  The operator class also specifies the support procedures to
   be used by
   the index method when the operator class is selected for an
   index column.  All the operators and functions used by an operator
   class must be defined before the operator class can be created.
  </P
><P
>   If a schema name is given then the operator class is created in the
   specified schema.  Otherwise it is created in the current schema.
   Two operator classes in the same schema can have the same name only if they
   are for different index methods.
  </P
><P
>   The user who defines an operator class becomes its owner.  Presently,
   the creating user must be a superuser.  (This restriction is made because
   an erroneous operator class definition could confuse or even crash the
   server.)
  </P
><P
>   <TT
CLASS="COMMAND"
>CREATE OPERATOR CLASS</TT
> does not presently check
   whether the operator class definition includes all the operators and
   functions required by the index method, nor whether the operators and
   functions form a self-consistent set.  It is the user's
   responsibility to define a valid operator class.
  </P
><P
>   Related operator classes can be grouped into <I
CLASS="FIRSTTERM"
>operator
   families</I
>.  To add a new operator class to an existing family,
   specify the <TT
CLASS="LITERAL"
>FAMILY</TT
> option in <TT
CLASS="COMMAND"
>CREATE OPERATOR
   CLASS</TT
>.  Without this option, the new class is placed into
   a family named the same as the new class (creating that family if
   it doesn't already exist).
  </P
><P
>   Refer to <A
HREF="xindex.html"
>Section 35.14</A
> for further information.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66351"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
>      The name of the operator class to be created.  The name can be
      schema-qualified.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>DEFAULT</TT
></DT
><DD
><P
>      If present, the operator class will become the default
      operator class for its data type.  At most one operator class
      can be the default for a specific data type and index method.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>data_type</I
></TT
></DT
><DD
><P
>      The column data type that this operator class is for.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>index_method</I
></TT
></DT
><DD
><P
>      The name of the index method this operator class is for.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>family_name</I
></TT
></DT
><DD
><P
>      The name of the existing operator family to add this operator class to.
      If not specified, a family named the same as the operator class is
      used (creating it, if it doesn't already exist).
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>strategy_number</I
></TT
></DT
><DD
><P
>      The index method's strategy number for an operator
      associated with the operator class.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>operator_name</I
></TT
></DT
><DD
><P
>      The name (optionally schema-qualified) of an operator associated
      with the operator class.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>op_type</I
></TT
></DT
><DD
><P
>      In an <TT
CLASS="LITERAL"
>OPERATOR</TT
> clause,
      the operand data type(s) of the operator, or <TT
CLASS="LITERAL"
>NONE</TT
> to
      signify a left-unary or right-unary operator.  The operand data
      types can be omitted in the normal case where they are the same
      as the operator class's data type.
     </P
><P
>      In a <TT
CLASS="LITERAL"
>FUNCTION</TT
> clause, the operand data type(s) the
      function is intended to support, if different from
      the input data type(s) of the function (for B-tree and hash indexes)
      or the class's data type (for GIN and GiST indexes).  These defaults
      are always correct, so there is no point in specifying <TT
CLASS="REPLACEABLE"
><I
>op_type</I
></TT
> in a <TT
CLASS="LITERAL"
>FUNCTION</TT
> clause
      in <TT
CLASS="COMMAND"
>CREATE OPERATOR CLASS</TT
>, but the option is provided
      for consistency with the comparable syntax in
      <TT
CLASS="COMMAND"
>ALTER OPERATOR FAMILY</TT
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>sort_family_name</I
></TT
></DT
><DD
><P
>      The name (optionally schema-qualified) of an existing <TT
CLASS="LITERAL"
>btree</TT
> operator
      family that describes the sort ordering associated with an ordering
      operator.
     </P
><P
>      If neither <TT
CLASS="LITERAL"
>FOR SEARCH</TT
> nor <TT
CLASS="LITERAL"
>FOR ORDER BY</TT
> is
      specified, <TT
CLASS="LITERAL"
>FOR SEARCH</TT
> is the default.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>support_number</I
></TT
></DT
><DD
><P
>      The index method's support procedure number for a
      function associated with the operator class.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>function_name</I
></TT
></DT
><DD
><P
>      The name (optionally schema-qualified) of a function that is an
      index method support procedure for the operator class.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>argument_type</I
></TT
></DT
><DD
><P
>      The parameter data type(s) of the function.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>storage_type</I
></TT
></DT
><DD
><P
>      The data type actually stored in the index.  Normally this is
      the same as the column data type, but some index methods
      (currently GIN and GiST) allow it to be different.  The
      <TT
CLASS="LITERAL"
>STORAGE</TT
> clause must be omitted unless the index
      method allows a different type to be used.
     </P
></DD
></DL
></DIV
><P
>   The <TT
CLASS="LITERAL"
>OPERATOR</TT
>, <TT
CLASS="LITERAL"
>FUNCTION</TT
>, and <TT
CLASS="LITERAL"
>STORAGE</TT
>
   clauses can appear in any order.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66437"
></A
><H2
>Notes</H2
><P
>   Because the index machinery does not check access permissions on functions
   before using them, including a function or operator in an operator class
   is tantamount to granting public execute permission on it.  This is usually
   not an issue for the sorts of functions that are useful in an operator
   class.
  </P
><P
>   The operators should not be defined by SQL functions.  A SQL function
   is likely to be inlined into the calling query, which will prevent
   the optimizer from recognizing that the query matches an index.
  </P
><P
>   Before <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 8.4, the <TT
CLASS="LITERAL"
>OPERATOR</TT
>
   clause could include a <TT
CLASS="LITERAL"
>RECHECK</TT
> option.  This is no longer
   supported because whether an index operator is <SPAN
CLASS="QUOTE"
>"lossy"</SPAN
> is now
   determined on-the-fly at run time.  This allows efficient handling of
   cases where an operator might or might not be lossy.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66446"
></A
><H2
>Examples</H2
><P
>   The following example command defines a GiST index operator class
   for the data type <TT
CLASS="LITERAL"
>_int4</TT
> (array of <TT
CLASS="TYPE"
>int4</TT
>).  See the
   <A
HREF="intarray.html"
>intarray</A
> module for the complete example.
  </P
><PRE
CLASS="PROGRAMLISTING"
>CREATE OPERATOR CLASS gist__int_ops
    DEFAULT FOR TYPE _int4 USING gist AS
        OPERATOR        3       &amp;&amp;,
        OPERATOR        6       = (anyarray, anyarray),
        OPERATOR        7       @&gt;,
        OPERATOR        8       &lt;@,
        OPERATOR        20      @@ (_int4, query_int),
        FUNCTION        1       g_int_consistent (internal, _int4, int, oid, internal),
        FUNCTION        2       g_int_union (internal, internal),
        FUNCTION        3       g_int_compress (internal),
        FUNCTION        4       g_int_decompress (internal),
        FUNCTION        5       g_int_penalty (internal, internal, internal),
        FUNCTION        6       g_int_picksplit (internal, internal),
        FUNCTION        7       g_int_same (_int4, _int4, internal);</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66453"
></A
><H2
>Compatibility</H2
><P
>   <TT
CLASS="COMMAND"
>CREATE OPERATOR CLASS</TT
> is a
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extension.  There is no
   <TT
CLASS="COMMAND"
>CREATE OPERATOR CLASS</TT
> statement in the SQL
   standard.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66459"
></A
><H2
>See Also</H2
><A
HREF="sql-alteropclass.html"
>ALTER OPERATOR CLASS</A
>, <A
HREF="sql-dropopclass.html"
>DROP OPERATOR CLASS</A
>, <A
HREF="sql-createopfamily.html"
>CREATE OPERATOR FAMILY</A
>, <A
HREF="sql-alteropfamily.html"
>ALTER OPERATOR FAMILY</A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createoperator.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-createopfamily.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>CREATE OPERATOR</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>CREATE OPERATOR FAMILY</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>